﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Net;
using System.IO;
using HRM.api;
using HRM.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using DTO;
using System.Reflection;
using System.Data;
namespace HRM.Controllers
{
    public class ReportController : Controller
    {
        //
        // GET: /Report/

        public ActionResult loadiExport() {
            List<CExport> list = new List<CExport>();
            CExport item;
            item = new CExport();
            item.id = 1;
            item.name = "Export To Excel";
            list.Add(item);
            item = new CExport();
            item.id = 2;
            item.name = "Export To PDF";
            list.Add(item);
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(list, settings),
                ContentType = "application/json"
            };

            return jsonResult;
        }

        public ActionResult gReportStaff() {
            ReportStaff item = new ReportStaff();
            item.id = 1;
            item.name = "Trình Độ";
            List<ReportStaff> list = new List<ReportStaff>();
            list.Add(item);
            item = new ReportStaff();
            item.id = 2;
            item.name = "Vụ";
            list.Add(item);
            item = new ReportStaff();
            item.id = 3;
            item.name = "Dưới 30 tuổi";
            list.Add(item);
            item = new ReportStaff();
            item.id = 4;
            item.name = "Trên 30 tuổi";
            list.Add(item);
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(list, settings),
                ContentType = "application/json"
            };

            return jsonResult;
        }

        public ActionResult loaiexport(int exportID)
        {
            Session[ManagerSessionName.LOAIEXPORT] = exportID;
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(exportID, settings),
                ContentType = "application/json"
            };
            return jsonResult;
        }

        public ActionResult exportNhanvien() {
            if (Session[ManagerSessionName.REPORT_NV] != null)
            {                
                List<NHANVIEN>list =(List<NHANVIEN>)Session[ManagerSessionName.REPORT_NV];
                String col = "ID,HONV,TENNV,PHAI,NGAYSINH,NOISINH,THANHDANH,DIACHI,NGHENGHIEP,SODIENTHOAI,NHAPMON,CHUYENMON";

                GridView grid = new GridView();                
                String[] cols = col.Split(',');
                DataTable tb = new DataTable();
                foreach (String m in cols)
                {
                    tb.Columns.Add(m);
                }

                foreach (NHANVIEN item in list)
                {
                    DataRow row = tb.NewRow();
                    foreach (PropertyInfo info in typeof(NHANVIEN).GetProperties())
                    {
                        if (this.isExists(info.Name, cols))
                        {
                            Object obj = info.GetValue(item, null);
                            if (obj != null)
                            {
                                row[info.Name] = obj.ToString();
                            }
                            else
                            {
                                row[info.Name] = obj;
                            }
                        }
                    }
                    tb.Rows.Add(row);
                }
                grid.DataSource = tb;
                grid.DataBind();
                String fileName = "nhanvien" + DateTime.Now.ToString("yyyy-mm-dd-hh-mm");                                
                int loaiexport = (int)Session[ManagerSessionName.LOAIEXPORT];
                switch (loaiexport)
                {
                    case 1:
                        {
                            fileName += ".xls";
                            Constant.ExportToExcel(grid, fileName);
                            break;
                        }
                    case 2:
                        {

                            fileName += ".pdf";
                            Constant.ExportToPDF(grid, fileName);
                            break;
                        }
                    default:
                        {
                            fileName += ".xls";
                            Constant.ExportToExcel(grid, fileName);
                            break;
                        }
                }
            }
            return View();
        }

      

        public ActionResult Index()
        {         
            return View();
        }
        private Boolean isExists(String name, String []list)
        {
            foreach (String m in list)
            {
                if (m.Trim() == name.Trim())
                    return true;
            }
            return false;
        }

        

        
        public ActionResult ReportTrinhDo(long tdid) {
            var objs = new NhanVienAPIController().Get().Where(p => p.TDID == tdid);
            Session[ManagerSessionName.REPORT_NV] = objs.ToList();
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(objs, settings),
                ContentType = "application/json"
            };

            return jsonResult;
        }

        public ActionResult ReportVu(long cvid)
        {
            var objs = new NhanVienAPIController().Get().Where(p => p.VUID == cvid);
            Session[ManagerSessionName.REPORT_NV] = objs.ToList();
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(objs, settings),
                ContentType = "application/json"
            };

            return jsonResult;
        }

        public ActionResult ReportDuoi30t()
        {
            DateTime dt = new DateTime(DateTime.Now.Year -30 ,1,1);
            var objs = new NhanVienAPIController().Get().Where(p => p.NGAYSINH >= dt);
            Session[ManagerSessionName.REPORT_NV] = objs.ToList();
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(objs, settings),
                ContentType = "application/json"
            };
            return jsonResult;
        }

        public ActionResult ReportTren30t()
        {
            DateTime dt = new DateTime(DateTime.Now.Year - 30, 1, 1);
            var objs = new NhanVienAPIController().Get().Where(p => p.NGAYSINH <= dt);
            Session[ManagerSessionName.REPORT_NV] = objs.ToList();
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(objs, settings),
                ContentType = "application/json"
            };
            return jsonResult;
        }

        public ActionResult ShowCol(){
            String col = "";
            bool flag = false;
            foreach (PropertyInfo info in typeof(NHANVIEN).GetProperties()) {
                if (flag == false)
                {
                    col = info.Name.Trim();
                    flag = true;
                }
                else
                {
                    col = col + "," + info.Name.Trim();
                }
            }
            var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
            var jsonResult = new ContentResult
            {
                Content = JsonConvert.SerializeObject(col, settings),
                ContentType = "application/json"
            };
            return jsonResult;
        }

       
    }
}
